package 力扣日常刷题.木23一月.第04天304;

import 力扣日常刷题.TreeNode;

/**
 * @author 帅小伙
 * @date 2022/3/4
 * @description
 */
public class Demo44求二叉搜索树k大的数 {

    int k;
    int ans = 0;

    public int kthLargest(TreeNode root, int k){
        this.k = k;
        dfs(root);
        return ans;
    }

    public void dfs(TreeNode root) {
        if(root == null) return;
        dfs(root.right);
        if(k == 0) return;
        if(--k == 0) ans = root.val;
        dfs(root.left);
    }

}
